u`
Ep.228uỎB؂v

p.228 ỎB؂

EB؂́uBcďɒ`悹vC[W
EhNXŊ{NXƓ̃oLqłd|
E̎AnewL[[hOu邱ƂŁuB؂ĂvƂ𖾎
 C#̃o[WɂĂnewLqȂĂB؂łꍇ邪Aقǂ

p.338 inheritance03

//p.338 inheritance03
using System;
class Base { //{NX
    public int x = 10;
    public void BaseMethod() {
        Console.WriteLine("BaseNXł");
    }
}
class Derived : Base { //hNX
    new public int x = 20; //f[^o̖ỎB؂
    new public void BaseMethod() { //\bh̖ỎB؂
        Console.WriteLine("DerivedNXł");
    }
}
class inheritance03 {
    public static void Main() {
        Derived d = new Derived(); //hNX̃CX^X𐶐
        Console.WriteLine("x = {0}", d.x); //hNXŉB؂f[^o𗘗p
        d.BaseMethod(); //hNXŉB؂\bh𗘗p
    }
}

AWKFp.338 inheritance03

Eprivatew(܂͖w)̃o̖ỎB؂ƁAprotectedw̃o̖ỎB؂̉ۂmF悤
 ǂ̏ꍇỎB؂͉\łAhNXpublicw肵Ă΁AOp\

쐬

//AWKFp.338 inheritance03
using System;
class Base { //{NX
    protected int x = 10;
    protected void BaseMethod() { //\bh̖ỎB؂
        Console.WriteLine("BaseNXł");
    }
}
class Derived : Base { //hNX
    new public int x = 20; //f[^o̖ỎB؂
    new public void BaseMethod() { //\bh̖ỎB؂
        Console.WriteLine("DerivedNXł");
    }
}
class inheritance03 {
    public static void Main() {
        Derived d = new Derived(); //hNX̃CX^X𐶐
        Console.WriteLine("x = {0}", d.x); //hNXŉB؂f[^o𗘗p
        d.BaseMethod(); //hNXŉB؂\bh𗘗p
    }
}

p.229ibaseL[[hj

EỎB؂͊{NXƓӖ̃oɓO^邱ƂŁAÕo̗
E܂u㏑vł͂Ȃ̂ŁAB؂̑ΏۂƂȂ{NX̃oubase.vOu邱Ƃŗp\

p.229 inheritance04.cs

//p.229 inheritance04.cs
using System;
class Base { //{NX
    protected int x = 10; //B؂f[^o
}
class Derived : Base { //hNX
    new int x = 20; //ỎB؂sw
    public void Show() {
        Console.WriteLine("base.x = {0}, x = {1} ", base.x, x); //{NX̃o𗘗p
    }
}
class inheritance04 {
    public static void Main() {
        Derived d = new Derived(); //hNX̃CX^X𐶐
        d.Show();
    }
}

p.230 \bh̃I[o[Ch

E\bhɂ閼ỎB؂̎@̈AỎB؂ł͎łȂԐ(q)\Ȏd|
EʏA\bhɂĂ̓I[o[Chp邱ƂB
EỎB؂Ƃ͈قȂA{NX̃\bhŃI[o[ChF߂邱ƂuvirtualvL[[hw肷Kviz\bhƂj
EhNXł̓I[o[Ch\bhł邱ƂAoverrideL[[hŖ
EI[o[Chł̓VOj`ɉăANZXCqvB
 ÓI\bh̃I[o[Ch͕siỎB؂͉\j

p.231 override01.cs

//p.231 override01.cs
using System;
class MyBase { //{NX
    public virtual void Method() { //I[o[Ch\ȉz\bh̒`
        Console.WriteLine("MyBase");
    }
}
class Derived1 : MyBase { //hNX
    public override void Method() {@//I[o[Ch\bh@̒`
        Console.WriteLine("Derived1");
    }
}
class Derived2 : MyBase { //hNX
    public override void Method() {@//I[o[Ch\bhA̒`
        Console.WriteLine("Derived2");
    }
}
class override01 {
    public static void Main() {
        Derived1 d1 = new Derived1();
        Derived2 d2 = new Derived2();
        d1.Method(); //I[o[Ch\bh@
        d2.Method(); //I[o[Ch\bhA
    }
}

AWKFp.231 override01.cs

EMyBaseNXDerived1NXɃf[^oǉĖỎB؂s
EMyBaseNXDerived2NXɐÓI\bhǉĖỎB؂s

//AWKFp.231 override01.cs
using System;
class MyBase { //{NX
    public int n; //yǉzf[^oiI[o[Chsj
    public virtual void Method() { //I[o[Ch\ȉz\bh̒`
        Console.WriteLine("MyBase");
    }
    public static void Methods() { //yǉzI[o[ChsȐÓI\bh̒`
        Console.WriteLine("MyBases");
    }
}
class Derived1 : MyBase { //hNX
    new public int n; //yǉzf[^o̖OB؂
    public override void Method() {@//I[o[Ch\bh@̒`
        Console.WriteLine("Derived1");
    }
}
class Derived2 : MyBase { //hNX
    public override void Method() {@//I[o[Ch\bhA̒`
        Console.WriteLine("Derived2");
    }
    new public void Methods() {@//yǉzÓI\bh̖ỎB؂
        Console.WriteLine("Derived2s");
    }
}
class override01 {
    public static void Main() {
        Derived1 d1 = new Derived1();
        Derived2 d2 = new Derived2();
        d1.Method(); //I[o[Ch\bh@
        d2.Methods(); //ÓI\bh̖ỎB؂
    }
}

p.232iԐ̐̑OɁc{NX̎Qƕϐɂāj

EhNX̃CX^X́A{NX̎QƕϐňƂł
EႦ΁uzC~XC̃zC~̓XCƂĈ邱ƁvƍlƗǂ
F
  class Slime {c}
  class HoimiSlime : Slime {c}
  F
  HoimiSlime hoimin = new HoimiSlime();
  Slime slime1go = hoimin; //pĂ̂Ō^͈ႤIuWFNgQƂł
ÊƂ𗘗pāA̔hNX̃IuWFNgA{NX^ƂĂ܂Ƃ߂Ĉ
EႦ΁uX^[heׂ̖͂ăX^[ňv
E̓Iɂ́A{NX^Ƃzɂ́AhNX̃CX^Xi[ł
F
  Slime[] slimes = {hoimin, behoimin, sulalin}; //hNX̃IuWFNg{NX̔z

p.232 Ԑ

EhNX̃IuWFNg{NX̌^łƁAỎB؂sĂꍇA{NXň
F
  class Slime {public void name(){ Console.WriteLine("XCł");}
  class HoimiSlime : Slime {new public void name(){ Console.WriteLine("zC~XCł");;}
  F
  Slime hoimin = new HoimiSlime();
  hoimin.name(); //uXCłvƕ\
EA͔hIuWFNgȂ̂AhNX̃\bh삷̂K
EꂪԐŁAI[o[Ch\bhɂĉ\ɂȂ
F
  class Slime {public virtual void name(){ Console.WriteLine("XCł");}
  class HoimiSlime : Slime {public override void name(){ Console.WriteLine("zC~XCł");;}
  F
  Slime hoimin = new HoimiSlime();
  hoimin.name(); //uzC~XCłvƕ\
EԐpƁA̔hNX̃IuWFNgA{NX^ƂĂ܂Ƃ߂ĈĂA̒`B
EĒʏA\bhɂĂ͖ỎB؂ł͂ȂI[o[ChpB
EԐɂA{NX̃\bhɑĔhNX̃I[o[Ch\bĥǂĂԂ͎sɌ܂̂ŁAI\bhfBXpb`ƂB

p.233 override02.cs

//p.233 override02.cs
using System;
class Mammal { //{NX(M)
    protected const int LegNo = 4; //r̐4{
    protected string Koe; //
    public virtual string Nakigoe() { //Ԃz\bh
        Koe = "..."; //Mނ̖͕sȂ̂Łu...vɂĂ
        return Koe; 
    }
    public int Leg() { //r̐Ԃʏ탁\bh
        return LegNo;
    }
}
class Cat : Mammal { //hNX(L)
    public override string Nakigoe() { //ԂI[o[Ch\bh
        Koe = "j[Aj["; //L̖ɂ
        return Koe;
    }
}
class Dog : Mammal { //hNX(L)
    public override string Nakigoe() { //ԂI[o[Ch\bh
        Koe = "A"; //̖ɂ
        return Koe;
    }
}
class override02 {
    public static void Main() {
        Mammal m; //MރNXƂ̔hNX߂̎Qƕϐ݂̂`
        Cat cat = new Cat(); //hNX(L)̃CX^X𐶐
        Dog dog = new Dog(); //hNX()̃CX^X𐶐
        m = cat; //hNX(L)̃CX^X{NX(M)ň
        Console.WriteLine("L̋r{0}{Ŗ́u{1}vł", 
            m.Leg(), m.Nakigoe()); //ʏ탁\bhƃI[o[Ch\bh(L)Ă
        m = dog; //hNX()̃CX^X{NX(M)ň
        Console.WriteLine("̋r{0}{Ŗ́u{1}vł",
            m.Leg(), m.Nakigoe()); //ʏ탁\bhƃI[o[Ch\bh()Ă
    }
}

AWKFp.233 override02.cs

E{NX^̔zɁAhNX^̂QCX^Xi[悤
Ez̑SvfɂČJԂŁAI[o[Ch\bhĂőԐmF悤

//AWKFp.233 override02.cs
using System;
class Mammal { //{NX(M)
    protected const int LegNo = 4; //r̐4{
    protected string Koe; //
    public virtual string Nakigoe() { //Ԃz\bh
        Koe = "..."; //Mނ̖͕sȂ̂Łu...vɂĂ
        return Koe; 
    }
    public int Leg() { //r̐Ԃʏ탁\bh
        return LegNo;
    }
}
class Cat : Mammal { //hNX(L)
    public override string Nakigoe() { //ԂI[o[Ch\bh
        Koe = "j[Aj["; //L̖ɂ
        return Koe;
    }
}
class Dog : Mammal { //hNX()
    public override string Nakigoe() { //ԂI[o[Ch\bh
        Koe = "A"; //̖ɂ
        return Koe;
    }
}
class override02 {
    public static void Main() {
        Cat cat = new Cat(); //hNX(L)̃CX^X𐶐
        Dog dog = new Dog(); //hNX()̃CX^X𐶐
        Mammal[] m = {cat, dog}; //MރNXƂ̔hNX߂̔zɊi[
        foreach (var w in m) { //Mނ̔z̑SvfɂČJԂ
            Console.WriteLine("r{0}{Ŗ́u{1}vł", 
            w.Leg(), w.Nakigoe()); //ʏ탁\bhƃI[o[Ch\bhĂ
        }
    }
}

p.235 vpeB̃I[o[Ch

E\bhƓlŁAԐł
E{NX̃vpeBzvpeBɂAhNX̃vpeBI[o[ChvpeBɂ

p.236 override03.cs

//p.236 override03.cs
using System;
class Mammal { //{NX(M)
    protected const int LegNo = 4; //r̐4{    
    public virtual string Nakigoe { //ԂzvpeB
        get { return "..."; } //setȂvpeB
    }
    public int Leg() { //r̐Ԃʏ탁\bh
        return LegNo;
    }
}
class Cat : Mammal { //hNX(L)
    public override string Nakigoe { //ԂI[o[ChvpeB
        get { return "j[Aj["; }
    }
}
class Dog : Mammal { //hNX()
    public override string Nakigoe { //ԂI[o[ChvpeB
        get { return "A"; }
    }
}
       
class override03 {
    public static void Main() {
        Mammal m; //MރNXƂ̔hNX߂̎Qƕϐ`
        Cat cat = new Cat(); //hNX(L)̃CX^X𐶐
        Dog dog = new Dog(); //hNX()̃CX^X𐶐
        m = cat; //hNX(L)̃CX^X{NX(M)ň
        Console.WriteLine("L̋r̐{0}{ŁA́u{1}vł",
            m.Leg(), m.Nakigoe); //ʏ탁\bhƃI[o[ChvpeB(L)Ă
        m = dog; //hNX()̃CX^X{NX(M)ň
        Console.WriteLine("̋r̐{0}{ŁA́u{1}vł",
            m.Leg(), m.Nakigoe); //ʏ탁\bhƃI[o[ChvpeB()Ă
    }
}

RQۑ聄 AWKFp.236 override03.cs

E{NX^̔zɁAhNX^̂QCX^Xi[悤
Ez̑SvfɂČJԂŁAI[o[ChvpeBĂőԐmF悤

p.237iI[o[ChCfNTj

E\bhƓlŁAԐł
E{NX̃CfNTzCfNTɂAhNX̃CfNTI[o[ChCfNTɂ

//p.237 override04.cs
using System;
class Mammal { //{NX(M)
    protected const int LegNo = 4; //r̐4{
    protected string Tail, Gei, Food, Koe; //ϐ
    public virtual string this[string index] { //ԂzCfNT
        get { return "..."; }
    }
    public int Leg() { //r̐Ԃʏ탁\bh
        return LegNo;
    }
}
class Cat : Mammal { //hNX(L)
    public override string this[string index] { //I[o[ChCfNT
        get {
            switch (index) { //CfbNX̒lɂԂI
                case "":
                    Tail = "1{";
                    return Tail;
                case "|":
                    Gei = "łȂ";
                    return Gei;
                case "":
                    Koe = "j[Aj[";
                    return Koe;
                case "Hו":
                    Food = "Lbgt[h";
                    return Food;
                default:
                    return "";
            }
        }
    }
}
class Dog : Mammal { //hNX()
    public override string this[string index] { //I[o[ChCfNT
        get {
            switch (index) { //CfbNX̒lɂԂI
                case "":
                    Tail = "1{";
                    return Tail;
                case "|":
                    Gei = "ł";
                    return Gei;
                case "":
                    Koe = "A";
                    return Koe;
                case "Hו":
                    Food = "hbOt[h";
                    return Food;
                default:
                    return "";
            }
        }
    }
}
class override04 {
    public static void Main() {
        Mammal m;
        Cat cat = new Cat();
        Dog dog = new Dog();
        m = cat;
        //CfbNXƂăI[oChCfNT(L)Ă
        Console.WriteLine("L̋r{0}{łB{1}łB|{2}BHו{3}B",
            m.Leg(), m[""], m["|"], m["Hו"]);
        m = dog;
        //CfbNXƂăI[oChCfNT()Ă
        Console.WriteLine("̋r{0}{łB{1}łB|{2}BHו{3}B",
            m.Leg(), m[""], m["|"], m["Hו"]);
    }
}

p.248 NX̑wKw

EhNX{NXƂĂɔh邱Ƃ\ieq̃C[Wj
E{NX̃o͔h̔hNXɂp
Eh̔hNX̃IuWFNg{NX̌^ňƂł
EhNXœƎɋLq\bhh̔hNXŃI[oChɂ́Az\bhɂ
EhNX̃I[oCh\bhh̔hNXŃI[oChɂ́AI[oCh\bhŗǂ

p.240 inheritance05.cs

//p.240 inheritance05.cs
using System;
class MyBase { //{NX
    protected int x = 10; //p\ȃf[^o
    public virtual void show() { //I[oCh\ȉz\bh
        Console.WriteLine("x = {0}", x);
    }
}
class Derived1 : MyBase { //hNX
    //Ɂuprotected int x = 10;vƂ݂Ȃ
    protected int y = 20; //p\ȃf[^o
    //Ɂupublic virtual void show() {c}vƂ݂Ȃ
}
class Derived2 : Derived1 { //h̔hNX
    //Ɂuprotected int x = 10;vƂ݂Ȃ
    //Ɂuprotected int y = 20;vƂ݂Ȃ
    int z = 30;
    public override void show() { //hNXp\bh̃I[oCh
        Console.WriteLine("z = {0}", z);
    }
}
class inheritance05 {
    public static void Main() {
        MyBase mb; //hNXƔh̔hNX߂̎Qƕϐ`
        Derived1 d1 = new Derived1(); //hNX̃CX^X𐶐
        Derived2 d2 = new Derived2(); //h̔hNX̃CX^X𐶐
        mb = d1; //hNX̃CX^X{NXň
        mb.show(); //ԐɂhNX̃\bh
        mb = d2; //h̔hNX̃CX^X{NXň
        mb.show(); //Ԑɂh̔hNX̃\bh
    }
}

p.243 NX̌pƃRXgN^iȂꍇj

Eǂ̃NXɂĂARXgN^SLqȂƁÂȂĝȂRXgN^Iɗpӂ
E{NX̃IuWFNgȂnewŐƁÂȂRXgN^Ă΂
EhNX̃IuWFNgȂnewŐƁF
@@ {NẌ̂ȂRXgN^Ă΂
@A hNẌ̂ȂRXgN^Ă΂
E邱ƂŁA{NXpf[^ȍȂǂ̏s
E܂A̔hNXɂ͕svł{NX̋@\pŕKvȏs
EāAh̔hNX̃IuWFNgȂnewŐƁF
@@ {NẌ̂ȂRXgN^Ă΂
@A hNẌ̂ȂRXgN^Ă΂
@B h̔hNẌ̂ȂRXgN^Ă΂

p.243 inheritance06.cs

//p.243 inheritance06.cs
using System;
class MyBase { //{NX
    protected int x; //p\ȃf[^o
    public MyBase() { //@̖RXgN^
        Console.WriteLine("MyBase");
        x = 10; //f[^ȍ
    }
}
class Derived1 : MyBase { //hNX
    //Ɂuprotected int x;vƂ݂Ȃ
    public Derived1() { //A̖RXgN^
        //̒OɊ{NẊ@̖RXgN^s
        Console.WriteLine("Derived1");
        x = 20; //f[^ȍ
    }
}
class Derived2 : Derived1 { //h̔hNX
    //Ɂuprotected int x;vƂ݂Ȃ
    public Derived2() {  //B̖RXgN^
        //̒OɊ{NẊ@̖RXgN^s
        //̒OɔhNẊA̖RXgN^s
        Console.WriteLine("Derived2");
        x = 30; //f[^ȍ
    }
    public void show() { //h̔hNX̓Ǝ̃\bh
        Console.WriteLine("x = {0}", x);
    }
}
class inheritance06 {
    public static void Main() {
        Derived2 d2 = new Derived2(); //h̔hNX̃CX^X𐶐(@AB̎s)
        d2.show(); //BŌɓ̂30ɂȂĂ
    }
}

p.245 {NX̃RXgN^Ƃꍇ

EႦ΁Ah̔hNX̃RXgN^̈uint ivƂAhNX̃RXgN^̈uint ivƂƃG[ɂȂ
ÉAhNX̃RXgN^̈^iȂ
ElɁA{NX̃RXgN^̈uint ivƂƃG[ɂȂ
ÉA{NX̃RXgN^̈^iȂ
ÊARXgN^ɂbaseL[[hɂn̎w
EF public NX(Xg) : base(,c){c}
EbaseL[[hɎw肷̓RXgN^̈pƗǂ
EF public Derived2(int i) : base(i) {c} //hNX̃RXgN^Ɉi̒ln
EF public Derived1(int i) : base(i) {c} //{NX̃RXgN^Ɉi̒ln

oFAWKFp.243 inheritance06.cs

ER̃NX̃RXgN^ׂ̈āuint ivɂ
Ex̏liŗ^Ƃ
EhNX̃RXgN^ƁAh̔hNX̃RXgN^baseL[[hǉ悤
EMainŔh̔hNX̃RXgN^Ɉ90^Ă݂悤

\Fp.245uinheritance07.csv

